/* Custom jquery function */
$.fn.flowUp = function(options) {
    var step = options["step"];
    var duration = options["duration"];
    var callback = options["callback"];

    $(this).animate({
        "position":"absolute",
        "top": "-=" + $(this).height() * step + "px"
    },
    duration,
    callback
    );
}
$.fn.flowLeft = function(options) {
    var step = options["step"];
    var duration = options["duration"];
    var callback = options["callback"];

    $(this).animate({
        "position":"absolute",
        "right": "+=" + $(this).width() * step + "px"
    },
    duration,
    callback
    );
}
$.fn.flowDown = function(options) {
    var step = options["step"];
    var duration = options["duration"];
    var callback = options["callback"];

    $(this).animate({
        "position":"absolute",
        "top": "+=" + $(this).height() * step + "px"
    },
    duration,
    callback
    );
}
$.fn.flowRight = function(options) {
    var step = options["step"];
    var duration = options["duration"];
    var callback = options["callback"];

    $(this).animate({
        "position":"absolute",
        "right": "-=" + $(this).width() * step + "px"
    },
    duration,
    callback
    );
}
/* Control function */
var CHILD_FD_PANELS = new Array();
var FD_CONTROL_BUTTONS = new Array();
var FD_PANEL_CONTROL = 1;
var CATEGORY_CONTROL = 2;
var LANGUAGE_CONTROL = 3;
$.fn.control = function(options){
    var type = options["type"];
    if(type == FD_PANEL_CONTROL){
        var panel = options["panel"];
        CHILD_FD_PANELS.push(panel);
        FD_CONTROL_BUTTONS.push(this);
        $(this).click(function(){
            if ($(panel).parent().is(':hidden') || (!$(panel).parent().is(':hidden') && $(panel).is(':hidden'))){
                for (var i=0; i < CHILD_FD_PANELS.length; i++) {
                    $(CHILD_FD_PANELS[i]).hide();
                }
                for (i=0; i < FD_CONTROL_BUTTONS.length; i++) {
                    $(FD_CONTROL_BUTTONS[i]).removeClass("active");
                }
                $(panel).show();
                $(this).addClass("active");
                if($(panel).parent().is(':hidden')){
                    $(panel).parent().slideToggle("fast");
                }
            }else if (!$(panel).parent().is(':hidden') && !$(panel).is(':hidden')){
                $(panel).parent().slideToggle("fast");
                for (i=0; i < CHILD_FD_PANELS.length; i++) {
                    $(CHILD_FD_PANELS[i]).hide();
                }
                $(this).removeClass("active");
            }
        });
    } else if(type == CATEGORY_CONTROL){
        $(this).click(function() {
            var subNav = options["subNav"];
            var width = $(this).width();
            $(subNav).css({
                width:width + "px",
                position: "absolute",
                bottom: $(subNav).parent().height() + 2 + "px"
            });
            $(subNav).slideToggle("normal");
        });
    } else if(type == LANGUAGE_CONTROL){
        var inputs = $(this);  
        $.each(inputs, function() {
            var pathName = $(this).attr('alt');
            $(this).click(function(){
                window.location.href = getDirectUrl(pathName);
            });
        });
    }
}
/*Expand & Collapse Panel*/
$.fn.expandable = function(options) {
    var expandablePanel = this;
    var expandButton = options["expandButton"];
    var contentPanel = options["contentPanel"];
    var minHeight = options["minHeight"];
    
    $(expandButton).click(function() {
        if($(expandablePanel).hasClass('collapsed')){
            var curHeight = $(expandablePanel).height();
            var autoHeight = $(expandablePanel).css('height', 'auto').height();
            $(expandablePanel).height(curHeight).animate({
                height: autoHeight
            },function(){
                $(expandablePanel).removeClass('collapsed');
            });
        }else{
            $(expandablePanel).addClass('collapsed');
            $(expandablePanel).animate({
                height: minHeight + "px"
            });
        }
    });
    
    $(window).resize(function() {
        if($(expandablePanel).height() > minHeight){
            if($(contentPanel).height() < $(expandablePanel).height()){
                var curHeight = $(expandablePanel).height()
                var autoHeight = $(expandablePanel).css('height', 'auto').height();
                $(expandablePanel).height(curHeight).animate({
                    height: autoHeight
                });
                $(expandablePanel).animate({
                    height: $(contentPanel).height()
                });
            }else{
                $(expandablePanel).addClass('collapsed');
                $(expandablePanel).animate({
                    height: $(contentPanel).height()
                },function(){
                    $(expandablePanel).removeClass('collapsed');
                });
            }
        }
    });
}
/* getDirectUrl */
function getDirectUrl(pathName){
    return window.location.protocol + "//" + window.location.host + pathName;
}
/* Search Panel */
$.fn.search = function() {
    if($(this).val()==""){
        return;
    }else{    
        var protocol = window.location.protocol;
        var host = window.location.host;
        var moduleName = window.location.pathname.split("/")[1];
        var url = protocol + "//" + host+"/"+moduleName+ "/1/" + $(this).val();
        window.location.href = url;
    }
}
/* Login Panel */
$.fn.login = function(options) {
    var loginUrl = options["loginUrl"];
    var email = options["email"];
    var password = options["password"];
    
    $.ajax({
        url: loginUrl,
        type: "POST",
        dataType: "json",
        data: {
            email: email,
            password: password
        },
        success: function (responseJson) {
            if(responseJson == null){
                location.reload();
            }else{
                alert(responseJson);
            }
        }
    });
}

function alertMessage(message){
    var dialogLayout = $("#modal-dialog");
    var dialog = $(".dialog");
    var msg = $(".errorMsg");
    $(msg).text(message);
    $(dialogLayout).show();
    $(dialog).css("margin-top", -$(dialog).height()/2 + "px");
}

/* Comment */
$.fn.loadComments = function(options) {
    var url = "/comment-engine/" + options.id + "/" + options.page;
    $.ajax({
        url: url,
        type: "POST",
        dataType: "json",
        success: function (responseJson) {
            if(responseJson != null){
                var html = "";
                for(var i=0; i<responseJson.result.length; i++){
                    html += "<div class='comment-item'>";
                    html += "<p class='comment-name'>";
                    html += responseJson.result[i].customer.fullName;               
                    html += "<span> (";
                    html += (new Date(responseJson.result[i].createdDate)).format("HH:MM");
                    html += " " + (new Date(responseJson.result[i].createdDate)).format("mmm d, yyyy");
                    html += ")</span>";
                    html += "</p>";
                    html += "<p class='comment-content'>";
                    html += responseJson.result[i].content;
                    html += "</p>";
                    html += "</div><!-- End .comment-item -->";
                }
                options.container.html(html);
                commentCurrentPage = responseJson.page;
                commentPageCount = responseJson.pageCount;
                var pageSize = responseJson.pageSize;
                if(responseJson.count <= pageSize){
                    $("#page-nav").hide();
                    return;
                }else{
                    $("#page-nav").show();
                }
                var from = (commentCurrentPage-1)*pageSize +1;
                var to = pageSize*commentCurrentPage;
                to = to > responseJson.count ? responseJson.count : to;
                var value = from == to ? from :from +"-"+ to;                
                $("#comment-page-number").text(value);
                $("#comment-page-count").text(responseJson.count);
            }
        }
    });
}
$.fn.sendComment = function(options){    
    if(options.content.val()==""){
        return;
    }else{
        var url = "/comment-engine/create/" + options.id;
        $.ajax({
            url: url,
            type: "POST",
            dataType: "json",
            data: {
                content: options.content.val(),
                locale: options.locale
            },
            success: function (responseJson) {
                if(responseJson == null){
                    options.content.val("").blur();
                    options.container.loadComments({
                        id: options.id,
                        page: options.page,
                        container: options.container,
                        next: options.next,
                        prev: options.prev
                    });                        
                }else{
                    alert(responseJson);

                }
            }
        });
    }
}
$.fn.autoWidth =  function(options){
    var width = 0;
    $(this).children().each(function(index, child){
        width = width+$(child).width()+options.margin;
    });
    $(this).css("width", width + "px");

}